﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Program.cs" company="">
//   
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
namespace AdminTool
{
    using System;
    using System.Security.Cryptography;
    using System.Threading;
    using System.Windows.Forms;

    using ExecutionTool.Profile;

    using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Unity;
    using Microsoft.Practices.ServiceLocation;
    using Microsoft.Practices.Unity;

    internal static class Program
    {
        #region Events

        internal static event EventHandler<ExportEnablementChangedEventArgs> ExportEnablementChanged = (sender, args) => { };

        #endregion

        #region Public Methods and Operators

        public static void OnExportEnablementChanged(ConfigExport configExport, ExportEnablementChangedEventArgs exportEnablementChangedEventArgs)
        {
            ExportEnablementChanged(configExport, exportEnablementChangedEventArgs);
        }

        #endregion

        #region Methods

        private static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
        {
            var validationEx = e.Exception as ValidationException;

            if (validationEx != null)
            {
                MessageBox.Show(e.Exception.Message, "Validation failed");
                return;
            }

            var cryptoEx = e.Exception as CryptographicException;
            if (cryptoEx != null && cryptoEx.Message == "Bad Data.\r\n")
            {
                MessageBox.Show(
                    string.Format(
                        "Encryption/Decryption error happended. Make sure you are not using a profile data created on another machine. --> {0}", 
                        e.Exception.Message), 
                    "Encryption/decryption failed");
                return;
            }

            MessageBox.Show(e.Exception.ToString(), "Error");
        }

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        private static void Main()
        {
            Application.ThreadException += Application_ThreadException;

            SetupServiceLocator();

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MainForm());
        }

        private static void SetupServiceLocator()
        {
            IUnityContainer container = new UnityContainer();
            IServiceLocator locator = new UnityServiceLocator(container);

            // Make sure this method call before any other type registrations.
            ServiceLocator.SetLocatorProvider(() => locator);

            container.AddNewExtension<EnterpriseLibraryCoreExtension>();
        }

        #endregion
    }
}
